package com.lkc.controller;

import com.lkc.bean.Comments;
import com.lkc.service.CommentsService;
import com.lkc.util.Page;
import com.lkc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    // 新增评论
    @PostMapping
    public Result add(@RequestBody Comments comments) {
        Comments obj = commentsService.add(comments);
        return Result.success(obj);
    }

    // 编辑评论
    @PutMapping
    public Result edit(@RequestBody Comments comments) {
        Comments obj = commentsService.edit(comments);
        return Result.success(obj);
    }

    // 删除评论
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id) {
        Object obj = commentsService.del(id);
        return Result.success(obj);
    }

    // 查询单条评论
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id) {
        Comments obj = commentsService.one(id);
        return Result.success(obj);
    }

    // 按商品ID查询评论列表
    @GetMapping("product/{productId}")
    public Result getCommentsByProduct(@PathVariable("productId") Integer productId) {
        List<Comments> list = commentsService.findByProductId(productId);
        return Result.success(list);
    }

    // 分页查询
    @GetMapping("page")
    public Result page(Comments comments, Page page) {
        commentsService.page(page, comments);
        return Result.success(page);
    }
}
